﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReferralTracker.Data.Contexts;
using ReferralTracker.BusinessObjects.Infrastructure;

namespace ReferralTracker.BusinessObjects {
    public abstract class BusinessObject : IDisposable {

        protected ISession _session = null;
        protected IContext _context = null;

        public BusinessObject() {
            if (ReferralTrackerSession.CurrentSession == null) {
                _session = null;
                _context = ReferralTrackerApplication.Resolve<IContext>();
            }
            else {
                _session = ReferralTrackerSession.CurrentSession;
                _context = _session.Context;
            }
        }

        public BusinessObject(ISession session) {
            _session = session;
            _context = session.Context;
        }

        protected virtual void SaveChanges() {
            if (_session == null) _context.SaveChanges();
        }

        public void Dispose() {
            if (_session == null) _context.Dispose();
        }
    }
}
